function datalog(i,bytes){
  var aa= (bytes[1+i]<<8 | bytes[2+i]);
  var bb= (bytes[3+i]<<8 | bytes[4+i]);
  var cc =  bytes[5+i]<<24>>24;
  
  var string='['+'MAJOR'+':'+aa+','+'MINOR'+':'+bb+','+'RSSI'+':'+cc+']'+',';  
  
  return string;
}

function decodeUplink(input) {
        return { 
            data: Decode(input.fPort, input.bytes, input.variables)
        };   
}
function Decode(fPort, bytes, variables) {

// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
	var i;
	var con;
	var con1;
	var con2;
	var str = "";
	var major = 1;
	var minor = 1;
	var rssi = 0;
	var power = 0;
	var device_information1 = 0;
	var device_information2 = 0;
	var device_information3 = 0;
	var addr = "";
	var alarm=0;//Alarm status
	var batV=0;//Battery,units:V
	var bat=0;//Battery,units:V
	var mod = 0;
	var led_updown="";//LED status for position,uplink and downlink
	var Firmware = 0;  // Firmware version; 5 bits   
	var hum=0;//hum,units: °
	var tem=0; //tem,units: °  
	var latitude=0;//gps latitude,units: °  
	var longitude = 0;//gps longitude,units: ° 
	var location=0;
	var time =0;
	var date =0;
  var sub_band;
  var freq_band;
  var sensor; 
  var firm_ver;	
  var sensor_mod;
  var gps_mod;
  var ble_mod;
  var pnackmd;
  var lon;
  var intwk;
  var BG;
  var location_tago = {};
  var rssi1m;
  var mac1;
  var mac2;
  var mac3;
  var rssi1 = 0;
  var rssi2 = 0;
  var rssi3 = 0;
	if(fPort == 2 || fPort == 3)
	{
		bat =(((bytes[8] & 0x3f) <<8) | bytes[9]);//Battery,units:V
		BG = bytes[10]>>3 & 0x01;
		latitude=(bytes[0]<<24 | bytes[1]<<16 | bytes[2]<<8 | bytes[3])/1000000;//gps latitude,units: °
		longitude=(bytes[4]<<24 | bytes[5]<<16 | bytes[6]<<8 | bytes[7])/1000000;//gps longitude,units: °
		if(BG == 1)
		{
		  var year = bytes[11] << 8 | bytes[12];
		  var month = bytes[13];
		  var day = bytes[14];
		  var hour = bytes[15];
		  var min = bytes[16];
		  var sec = bytes[17];

		  // Crear la fecha y hora en formato UTC
		  var utcDate = new Date(Date.UTC(year, month - 1, day, hour, min, sec));

		  // Convertir la fecha y hora a GMT-5
		  var gmtMinus5Date = new Date(utcDate.getTime() - 0 * 60 * 60 * 1000);

		  // Obtener los componentes de la fecha y hora en GMT-5
		  var gmtMinus5Year = gmtMinus5Date.getFullYear();
		  var gmtMinus5Month = gmtMinus5Date.getMonth() + 1;
		  var gmtMinus5Day = gmtMinus5Date.getDate();
		  var gmtMinus5Hour = gmtMinus5Date.getHours();
		  var gmtMinus5Min = gmtMinus5Date.getMinutes();
		  var gmtMinus5Sec = gmtMinus5Date.getSeconds();

		  var date = gmtMinus5Year + '-' + gmtMinus5Month + '-' + gmtMinus5Day;
		  var time = gmtMinus5Hour + ':' + gmtMinus5Min + ':' + gmtMinus5Sec;
		}

		if ((latitude < 190) && (latitude > -190)) {
				if ((longitude < 190) && (longitude > -190)) {
					if ((latitude !== 0) && (longitude !==0)) {
							field: "location",
						  location= "" + latitude + "," + longitude + "";
					} 
			   }
		}
		else
		location_tago="invalid value";
		
		if ((latitude < 190) && (latitude > -190)) {
				if ((longitude < 190) && (longitude > -190)) {
					if ((latitude !== 0) && (longitude !==0)) {	
		          location_tago.latitude=(bytes[0]<<24 | bytes[1]<<16 | bytes[2]<<8 | bytes[3])/1000000;//gps latitude,units: °
		          location_tago.longitude=(bytes[4]<<24 | bytes[5]<<16 | bytes[6]<<8 | bytes[7])/1000000;//gps longitude,units: °
					}
				}
		}
		else
		location_tago="invalid value";
					
		alarm=(bytes[8] & 0x40)?"TRUE":"FALSE";//Alarm status
		batV=(((bytes[8] & 0x3f) <<8) | bytes[9])/1000;//Battery,units:V
		mod = bytes[10] & 0xC0;

		if(mod !== 1 && BG == 0) 
		{
		 hum=(bytes[11]<<8 | bytes[12])/10;//hum,units: °
		 tem=(bytes[13]<<24>>16 | bytes[14])/100; //tem,units: °   
		}
		led_updown=(bytes[10] & 0x20)?"ON":"OFF";//LED status for position,uplink and downlink
	    intwk = (bytes[10] & 0x10)?"MOVE":"STILL";
			
			if(fPort == 2)
			{
				return {
				  Location: location,
				  Latitude: latitude,
				  Longitud: longitude,
				  location_tago:location_tago,
				  Hum:hum,
				  Tem:tem,
				  BatV:batV,
				  ALARM_status:alarm,
				  MD:mod,
				  Bg:BG,
				  Date: date,
				  Time: time,			  
				  LON:led_updown,
				  Transport:intwk,
				  };		
			}
			else if(fPort == 3)
			{
				return {
				  Location: location,
				  Latitude: latitude,
				  Longitud: longitude,
				  location_tago:location_tago,
				  BatV:batV,
				  ALARM_status:alarm,
				  MD:mod,
				  LON:led_updown,
				  Transport:intwk,
				  };		
			}	
	}

	if (fPort == 0x04) 
	{
		var latitude = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]) / 1000000; //gps latitude,units: °
		var longitude = (bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7]) / 1000000; //gps longitude,units: °


		if ((latitude < 190) && (latitude > -190)) {
				if ((longitude < 190) && (longitude > -190)) {
					if ((latitude !== 0) && (longitude !==0)) {
							field: "location",
						  location= "" + latitude + "," + longitude + "";
					} 
			   }
		}
		else
		location="invalid value";
		if ((latitude < 190) && (latitude > -190)) {
				if ((longitude < 190) && (longitude > -190)) {
					if ((latitude !== 0) && (longitude !==0)) {	
						location_tago.latitude=(bytes[0]<<24 | bytes[1]<<16 | bytes[2]<<8 | bytes[3])/1000000;//gps latitude,units: °
						location_tago.longitude=(bytes[4]<<24 | bytes[5]<<16 | bytes[6]<<8 | bytes[7])/1000000;//gps longitude,units: °
						}
					}
		}
		else
		location_tago="invalid value";

		var year = bytes[8] << 8 | bytes[9];
		var month = bytes[10];
		var day = bytes[11];
		var hour = bytes[12];
		var min = bytes[13];
		var sec = bytes[14];

		// Crear la fecha y hora en formato UTC
		var utcDate = new Date(Date.UTC(year, month - 1, day, hour, min, sec));

		// Convertir la fecha y hora a GMT-5
		var gmtMinus5Date = new Date(utcDate.getTime() - 0 * 60 * 60 * 1000);

		// Obtener los componentes de la fecha y hora en GMT-5
		var gmtMinus5Year = gmtMinus5Date.getFullYear();
		var gmtMinus5Month = gmtMinus5Date.getMonth() + 1;
		var gmtMinus5Day = gmtMinus5Date.getDate();
		var gmtMinus5Hour = gmtMinus5Date.getHours();
		var gmtMinus5Min = gmtMinus5Date.getMinutes();
		var gmtMinus5Sec = gmtMinus5Date.getSeconds();

		var date = gmtMinus5Year + '-' + gmtMinus5Month + '-' + gmtMinus5Day;
		var time = gmtMinus5Hour + ':' + gmtMinus5Min + ':' + gmtMinus5Sec;

		return {
			Location: location,
			Latitude: latitude,
			Longitud: longitude,
			location_tago:location_tago,
			Date: date,
			Time: time,
		};
	}
	if(fPort == 0x07)
	{
		alarm=(bytes[0] & 0x40)?"TRUE":"FALSE";//Alarm status
		batV=(((bytes[0] & 0x3f) <<8) | bytes[1])/1000;//Battery,units:V
		mod = bytes[2] & 0xC0;
		led_updown=(bytes[2] & 0x20)?"ON":"OFF";//LED status for position,uplink and downlink
		return {
		  BatV:batV,
		  ALARM_status:alarm,
		  MD:mod,
		  LON:led_updown,
		  };				
	}
	if(fPort==0x08)
	{
		con = "";
		for(i = 0 ; i < 6 ; i++) {
			con = bytes[i].toString();
			str += String.fromCharCode(con);
		}
		var wifissid = str,
		rssi =  bytes[6]<<24>>24;
		alarm=(bytes[7] & 0x40)?"TRUE":"FALSE";//Alarm status
		batV=(((bytes[7] & 0x3f) <<8) | bytes[8])/1000;//Battery,units:V
		mod = (bytes[9] & 0xC0)>>6;
		led_updown=(bytes[9] & 0x20)?"ON":"OFF";//LED status for position,uplink and downlink
		return {
		  WIFISSID:wifissid,
		  RSSI:rssi,
		  BatV:batV,
		  ALARM_status:alarm,	
		  MD:mod,
		  LON:led_updown,			  
		  };	  
	}
	if(fPort==0x05)
	{
		if(bytes[0]==0x13)
		  sensor_mode="TrackerD";
		else
		  sensor_mode="NULL"; 
		   
		if(bytes[4]==0xff)
		  sub_band="NULL";
		else
		  sub_band=bytes[4];
		
		if(bytes[3]==0x01)
		  freq_band="EU868";
		else if(bytes[3]==0x02)
		  freq_band="US915";
		else if(bytes[3]==0x03)
		  freq_band="IN865";
		else if(bytes[3]==0x04)
		  freq_band="AU915";
		else if(bytes[3]==0x05)
		  freq_band="KZ865";
		else if(bytes[3]==0x06)
		  freq_band="RU864";
		else if(bytes[3]==0x07)
		  freq_band="AS923";
		else if(bytes[3]==0x08)
		  freq_band="AS923_1";
		else if(bytes[3]==0x09)
		  freq_band="AS923_2";
		else if(bytes[3]==0x0A)
		  freq_band="AS923_3";
		else if(bytes[3]==0x0B)
		  freq_band="CN470";
		else if(bytes[3]==0x0C)
		  freq_band="EU433";
		else if(bytes[3]==0x0D)
		  freq_band="KR920";
		else if(bytes[3]==0x0E)
		  freq_band="MA869";
		  
		firm_ver= (bytes[1]&0x0f)+'.'+(bytes[2]>>4&0x0f)+'.'+(bytes[2]&0x0f);
		batV= (bytes[5]<<8 | bytes[6])/1000;
		semsor_mod = (bytes[7]>>6)&0x3f;
		gps_mod = (bytes[7]>>4)&0x03;
		ble_mod = bytes[7]&0x0f;
		panackmd = bytes[8]&0x04;
		lon = ((bytes[8]>>1)&0x01)?"ON":"OFF";;
		intwk = bytes[8]&0x01;
		
		if(semsor_mod == 1)
		   sensor= "GPS";
		else if(semsor_mod == 2)
		   sensor= "BLE"; 
		else if(intwk == 1)
		   sensor= "Spots";         
		 else if(semsor_mod == 3)
		   sensor= "BLE+GPS Hybrid";     
		return {
		BatV:batV,
		SENSOR_MODEL:sensor_mode,
		FIRMWARE_VERSION:firm_ver,
		FREQUENCY_BAND:freq_band,
		SUB_BAND:sub_band, 
		SMODE:sensor,
		GPS_M0D:gps_mod,
		BLE_MD:ble_mod,
		PNACKMD:pnackmd,
		LON:lon,
		Intwk:intwk,
		};
	}	
	if(fPort==0x06)
	{
		major =  bytes[16] << 8 | bytes[17];
		
		minor =  bytes[18] << 8 | bytes[19];
		
		power = bytes[15];
		
		rssi1m = bytes[21]<<24>>24;
		
		rssi =  bytes[23]<<24>>24;

		con = "";
		for(i = 0 ; i < 16 ; i++) {
			con += bytes[i].toString(16);
		}
		value =  con;
		var uuid = value;	
		alarm=(bytes[24] & 0x40)?"TRUE":"FALSE";//Alarm status
		batV=(((bytes[24] & 0x3f) <<8) | bytes[25])/1000;//Battery,units:V
		mod = (bytes[26] & 0xC0)>>6;
		led_updown=(bytes[26] & 0x20)?"ON":"OFF";//LED status for position,uplink and downlink
		if(bytes[26] & 0xC0==0x40) 
		{
			hum=(bytes[27]<<8 | bytes[28])/10;//hum,units: °
			tem=(bytes[29]<<24>>16 | bytes[30])/100; //tem,units: °
		}
		return {
		  BatV:batV,
		  ALARM_status:alarm,
		  MD:mod,
		  LON:led_updown,
		  UUID: uuid,
		  MAJOR: major,
		  MINOR: minor,
		  RSSI:rssi,
		  RSSI1M:rssi1m,
		  POWER:power,
		  };			
	}
	if(fPort==0x0A)
	{
	  function bytesToHex(bytes) {
    return bytes.map(byte => byte.toString(16).padStart(2, '0')).join('');
}
		const MAC1 = bytesToHex(bytes.slice(0, 6));
		
		const RSSI1 = bytes[6]<<24>>24;
		
		const MAC2 = bytesToHex(bytes.slice(7, 13));
		
		const RSSI2 =  bytes[13]<<24>>24;

		const MAC3 = bytesToHex(bytes.slice(14, 20));
		
		const RSSI3 =  bytes[20]<<24>>24;
		
		alarm=(bytes[21] & 0x40)?"TRUE":"FALSE";//Alarm status
		batV=(((bytes[21] & 0x3f) <<8) | bytes[22])/1000;//Battery,units:V
		mod = (bytes[23] & 0xC0)>>6;
		return {
		  BatV:batV,
		  ALARM_status:alarm,
		  MD:mod,
		  MAC1:MAC1,
		  MAC2: MAC2,
		  MAC3: MAC3,
		  RSSI1: RSSI1,
		  RSSI2:RSSI2,
		  RSSI3:RSSI3,
		  };		
	 
	}
}